using QxFramework.Core;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
public class Cell : UIBase
{
    public Item Item;

    private Image _sprite;
    private Button _button;
    private PlayerControl _playerControl;
    private BagManager _bagManager;
    private BagMenu _bagMenu;
    //public override void OnDisplay(object args)
    //{
    //    base.OnDisplay(args);
    //    _button = transform.GetComponent<Button>();
    //    _button.onClick.AddListener(UseObject);
    //    _playerControl = GameMgr.Get<IPlayerManager>().Player;
    //}
    public void RefreshSprite()
    {
        if(_sprite == null)
            _sprite = transform.GetChild(0).GetComponent<Image>();
        if (Item == null)
        {
            _sprite.sprite = null;
            _sprite.color = new Color(255, 255, 255, 0);
        }
        else
        {
            _sprite.sprite = Item.icon;
            _sprite.color = new Color(255, 255, 255, 255);
        }
    }
    void UseObject()
    {
        if (Item == null) return;

        if (transform.tag == "BagCell")
        {
            int i = -1;
            if (Item is Weapon)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (_bagManager.Weapon[j] == null) { if (i == -1) i = j; }
                    else if (_bagManager.Weapon[j] == Item) return;
                }
                if (i == -1) return;
                _bagManager.Weapon[i] = (Weapon)Item;
                _playerControl.weapon[i] = (Weapon)Item;
            }
            else if (Item is Prop)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (_bagManager.Prop[j] == null) { if (i == -1) i = j; }
                    else if (_bagManager.Prop[j] == Item && _playerControl.reportProp[j] == transform.name) return;
                }
                if (i == -1) return;
                _playerControl.reportProp[i] = transform.name;
                _bagManager.Prop[i] = (Prop)Item;
                _playerControl.prop[i] = (Prop)Item;
            }
        }
        else if (transform.tag == "EquipCell")
        {
            if (Item is Weapon)
                for (int i = 0; i < 4; i++)
                {
                    if (_bagManager.Weapon[i] == Item)
                    {
                        if (_playerControl.currentWeapon == Item)
                            return;
                        _bagManager.Weapon[i] = null;
                        _playerControl.weapon[i] = null;
                        break;
                    }
                }
            else if (Item is Prop)
                for (int i = 0; i < 4; i++)
                {
                    if (_bagManager.Prop[i] == Item)
                    {
                        _playerControl.reportProp[i] = null;
                        _bagManager.Prop[i] = null;
                        _playerControl.prop[i] = null;
                        break;
                    }
                }
        }

        _bagManager = GameObject.Find("BagMgr").GetComponent<BagManager>();
        _bagMenu = UIManager.Instance.GetUI("BagMenu").GetComponent<BagMenu>();
        _bagManager.RefreshWeapon();
        _bagMenu.RefreshWeaponCell();
        _bagManager.RefreshProp();
        _bagMenu.RefreshPropCell();

    }


    private float OpenBeforeTime = 0.5f;
    private float openBeforeTime;
    private bool isItemMenuOpen = false;
    private RectTransform _rectTransform;
    private Vector2 mousePosition;
    void Start()
    {
        _rectTransform = GetComponent<RectTransform>();
        openBeforeTime = OpenBeforeTime;
        _bagManager = GameObject.Find("BagMgr").GetComponent<BagManager>();
        _button = transform.GetComponent<Button>();
        _button.onClick.AddListener(UseObject);
        _playerControl = GameMgr.Get<IPlayerManager>().Player;
    }
    void Update()
    {
        if (Item != null)
        {
            RectTransformUtility.ScreenPointToLocalPointInRectangle(_rectTransform, Input.mousePosition,null, out mousePosition);
            if (_rectTransform.rect.Contains(mousePosition))
            {
                if (openBeforeTime > 0) openBeforeTime -= Time.deltaTime;
                else if (!isItemMenuOpen)
                {
                    isItemMenuOpen = true;
                    UIManager.Instance.Open("ItemMenu", "", Item);
                }
            }
            else if (isItemMenuOpen)
            {
                isItemMenuOpen = false;
                openBeforeTime = OpenBeforeTime;
                UIManager.Instance.Close("ItemMenu");
            }
        }
        else if (isItemMenuOpen)
        {
            isItemMenuOpen = false;
            openBeforeTime = OpenBeforeTime;
            UIManager.Instance.Close("ItemMenu");
        }
    }
}
